import  ajax  from "../../utils/ajax2";
const dataList = document.getElementById('dataList');
const navigation = document.getElementById('navigation');
const modal = document.getElementById('modal');
let pageTotal;// 页总数
let pageCurrent = 1;// 当前页数
let pageSize = 15;// 每页显示文档数

getGoods();
function getGoods() {
  ajax('get', `/api/users/getGoods`, { pageCurrent, pageSize }, localStorage.getItem('token')).then(result => {
    if (result.flag) {
       //遍历数据库数据,循环渲染至页面
      pageTotal = result.pageTotal;
      page(pageTotal);
      console.log(result);
      dataList.innerHTML = '';
      result.data.forEach(item => {
        const tr = document.createElement('tr');

        let td = document.createElement('td');
        td.innerText = item.name
        tr.appendChild(td);

        td = document.createElement('td');
        const path = item.img.replace(/\\/g ,'/');
        let p = path.slice(7);
        console.log(p);

        td.innerHTML= `<img src = http://127.0.0.1:3000/${p} >`;
        tr.appendChild(td);

        td = document.createElement('td');
        td.innerText = item.describe;
        tr.appendChild(td);

        td = document.createElement('td');
        td.innerText = item.price;
        tr.appendChild(td);

        td = document.createElement('td');
        td.innerText = item.fineness;
        tr.appendChild(td);

        td = document.createElement('td');
        td.innerText = item.classify;
        tr.appendChild(td);

        td = document.createElement('td');
        td.innerText = item.createdAt;
        tr.appendChild(td);

        td = document.createElement('td');
        const btnDelete = document.createElement('a');
        btnDelete.innerText = '删除';
        btnDelete.dataset.id = item._id;
        btnDelete.onclick = btnDelete_click;
        td.appendChild(btnDelete)
        tr.appendChild(td);

        dataList.appendChild(tr);

         // 事件委托
         let i=0;
      navigation.onclick = function (e) {
        if (e.target.nodeName == 'A') {
          
          const index = e.target.dataset.index * 1;
          index < 1 ? index = 1 : index;
          index > pageTotal ? index = pageTotal : index;
          pageCurrent = index;
          alert(`第${pageCurrent+i}页`)
          location.assign('goodsList.html')
        }
      }
      });

    }
  })
}

  // 设置翻页组件
  function page(data) {
    let s = '';

    s += `<a data-index='1'>首页</a>`;
    s += `<a data-index='${pageCurrent - 1}'>上一页</a>`;

    for (let i = 1; i <= pageTotal; i++) {
      s += `<a data-index='${i}'>${i}</a>`;
    }

    s += `<a data-index='${pageCurrent + 1}'>下一页</a>`;
    s += `<a data-index='${pageTotal}'>尾页</a>`;

    navigation.innerHTML = s;

    const a = navigation.children;
    for (let i = 1; i < a.length - 1; i++) {
      if (a[i].dataset.index == pageCurrent) {
        a[i].style.backgroundColor = 'pink';
      }
    }
  }


function btnDelete_click() {
  if (confirm('您确定要删除吗？')) {
    ajax('get', '/api/users/deleteGoods', { id: this.dataset.id }, localStorage.getItem('token')).then(result => {
      alert(result.msg);
      if (result.flag) {
        getGoods();
      }
    })
  }
}
